import { Global } from "../../../gengine/global/Global";
//class JSON
    
    export  class JSON
    {
       

        public static deserialize(arg1: string)
        {
            var source: string;
            var at: number;
            var ch: string;
            var _isDigit: Function;
            var _isHexDigit: Function;
            var _white: Function;
            var _string: Function;
            var _next: Function;
            var _array: Function;
            var _object: Function;
            var _number: Function;
            var _word: Function;
            var _value: Function;
            var _error: Function;

            var loc1;
            at = NaN;
            ch = null;
            _isDigit = null;
            _isHexDigit = null;
            _white = null;
            _string = null;
            _next = null;
            _array = null;
            _object = null;
            _number = null;
            _word = null;
            _value = null;
            _error = null;
            source = arg1;
            source = new string(source);
            at = 0;
            ch = " ";
            _isDigit = (arg1: string)
            {
                return "0" <= arg1 && arg1 <= "9";
            }
            _isHexDigit = (arg1: string)
            {
                return _isDigit(arg1) || "A" <= arg1 && arg1 <= "F" || "a" <= arg1 && arg1 <= "f";
            }
            _error = (arg1: string): void
            {
                if (Global.isDebugModle) 
                {
                    throw new Error(arg1, (at - 1));
                }
                return;
            }
            _next = ()
            {
                ch = source.charAt(at);
                at = at + 1;
                return ch;
            }
            _white = (): void
            {
                while (ch) 
                {
                    if (ch <= " ") 
                    {
                        _next();
                        continue;
                    }
                    if (ch == "/") 
                    {
                        var loc1=_next();
                        switch (loc1) 
                        {
                            case "/":
                            {
                                do 
                                {
                                }
                                while (_next() && !(ch == "\n") && !(ch == "\r"));
                                break;
                            }
                            case "*":
                            {
                                _next();
                                for (;;) 
                                {
                                    if (ch) 
                                    {
                                        if (ch != "*") 
                                        {
                                            _next();
                                        }
                                        else if (_next() == "/") 
                                        {
                                            _next();
                                            break;
                                        }
                                        continue;
                                    }
                                    _error("Unterminated Comment");
                                }
                                break;
                            }
                            default:
                            {
                                _error("Syntax Error");
                            }
                        }
                        continue;
                    }
                    break;
                }
                return;
            }
            _string = ()
            {
                var loc3=undefined;
                var loc4=undefined;
                var loc1="";
                var loc2="";
                var loc5=false;
                if (ch == "\"") 
                {
                    while (_next()) 
                    {
                        if (ch == "\"") 
                        {
                            _next();
                            return loc2;
                        }
                        if (ch == "\\") 
                        {
                            var loc6=_next();
                            switch (loc6) 
                            {
                                case "b":
                                {
                                    loc2 = loc2 + "";
                                    break;
                                }
                                case "f":
                                {
                                    loc2 = loc2 + "";
                                    break;
                                }
                                case "n":
                                {
                                    loc2 = loc2 + "\n";
                                    break;
                                }
                                case "r":
                                {
                                    loc2 = loc2 + "\r";
                                    break;
                                }
                                case "t":
                                {
                                    loc2 = loc2 + "\t";
                                    break;
                                }
                                case "u":
                                {
                                    loc4 = 0;
                                    loc1 = 0;
                                    while (loc1 < 4) 
                                    {
                                        loc3 = parseInt(_next(), 16);
                                        if (!isFinite(loc3)) 
                                        {
                                            loc5 = true;
                                            break;
                                        }
                                        loc4 = loc4 * 16 + loc3;
                                        loc1 = loc1 + 1;
                                    }
                                    if (loc5) 
                                    {
                                        loc5 = false;
                                        break;
                                    }
                                    loc2 = loc2 + String.fromCharCode(loc4);
                                    break;
                                }
                                default:
                                {
                                    loc2 = loc2 + ch;
                                }
                            }
                            continue;
                        }
                        loc2 = loc2 + ch;
                    }
                }
                _error("Bad String");
                return null;
            }
            _array = ()
            {
                var loc1=[];
                if (ch == "[") 
                {
                    _next();
                    _white();
                    if (ch == "]") 
                    {
                        _next();
                        return loc1;
                    }
                    while (ch) 
                    {
                        loc1.push(_value());
                        _white();
                        if (ch == "]") 
                        {
                            _next();
                            return loc1;
                        }
                        if (ch == ",") 
                        {
                        };
                        _next();
                        _white();
                    }
                }
                _error("Bad Array");
                return null;
            }
            _object = ()
            {
                var loc1={};
                var loc2={};
                if (ch == "{") 
                {
                    _next();
                    _white();
                    if (ch == "}") 
                    {
                        _next();
                        return loc2;
                    }
                    while (ch) 
                    {
                        loc1 = _string();
                        _white();
                        if (ch == ":") 
                        {
                        };
                        _next();
                        loc2[loc1] = _value();
                        _white();
                        if (ch == "}") 
                        {
                            _next();
                            return loc2;
                        }
                        if (ch == ",") 
                        {
                        };
                        _next();
                        _white();
                    }
                }
                _error("Bad Object");
                return;
            }
            _number = ()
            {
                var loc2=undefined;
                var loc1="";
                var loc3="";
                var loc4="";
                if (ch == "-") 
                {
                    loc1 = "-";
                    loc4 = loc1;
                    _next();
                }
                if (ch == "0") 
                {
                    _next();
                    if (ch == "x" || ch == "X") 
                    {
                        _next();
                        while (_isHexDigit(ch)) 
                        {
                            loc3 = loc3 + ch;
                            _next();
                        }
                        if (loc3 != "") 
                        {
                            return Number(loc4 + "0x" + loc3);
                        }
                        else 
                        {
                            _error("mal formed Hexadecimal");
                        }
                    }
                    else 
                    {
                        loc1 = loc1 + "0";
                    }
                }
                while (_isDigit(ch)) 
                {
                    loc1 = loc1 + ch;
                    _next();
                }
                if (ch == ".") 
                {
                    loc1 = loc1 + ".";
                    while (_next() && ch >= "0" && ch <= "9") 
                    {
                        loc1 = loc1 + ch;
                    }
                }
                loc2 = 1 * loc1;
                if (isFinite(loc2)) 
                {
                    return loc2;
                }
                else 
                {
                    _error("Bad Number");
                }
                return NaN;
            }
            _word = ()
            {
                var loc1=ch;
                switch (loc1) 
                {
                    case "t":
                    {
                        if (_next() == "r" && _next() == "u" && _next() == "e") 
                        {
                            _next();
                            return true;
                        }
                        break;
                    }
                    case "f":
                    {
                        if (_next() == "a" && _next() == "l" && _next() == "s" && _next() == "e") 
                        {
                            _next();
                            return false;
                        }
                        break;
                    }
                    case "n":
                    {
                        if (_next() == "u" && _next() == "l" && _next() == "l") 
                        {
                            _next();
                            return null;
                        }
                        break;
                    }
                }
                _error("Syntax Error");
                return null;
            }
            _value = ()
            {
                _white();
                var loc1=ch;
                switch (loc1) 
                {
                    case "{":
                    {
                        return _object();
                    }
                    case "[":
                    {
                        return _array();
                    }
                    case "\"":
                    {
                        return _string();
                    }
                    case "-":
                    {
                        return _number();
                    }
                    default:
                    {
                        return ch >= "0" && ch <= "9" ? _number() : _word();
                    }
                }
            }
            return _value();
        }

        public static serialize(arg1): string
        {
            var loc1=null;
            var loc2=NaN;
            var loc3=NaN;
            var loc5=undefined;
            var loc6=null;
            var loc7=NaN;
            var loc4="";
            var loc8=typeof arg1;
            switch (loc8) 
            {
                case "object":
                {
                    if (arg1) 
                    {
                        if (typeof arg1 === "array") 
                        {
                            loc3 = arg1.length;
                            loc2 = 0;
                            while (loc2 < loc3) 
                            {
                                loc5 = JSON.serialize(arg1[loc2]);
                                if (loc4) 
                                {
                                    loc4 = loc4 + ",";
                                }
                                loc4 = loc4 + loc5;
                                ++loc2;
                            }
                            return "[" + loc4 + "]";
                        }
                        if (typeof arg1.toString != "undefined") 
                        {
                            loc8 = 0;
                            var loc9=arg1;
                            for (loc6 in loc9) 
                            {
                                if (!(!(typeof (loc5 = arg1[loc6]) == "undefined") && !(typeof loc5 == "function"))) 
                                {
                                    continue;
                                }
                                loc5 = JSON.serialize(loc5);
                                if (loc4) 
                                {
                                    loc4 = loc4 + ",";
                                }
                                loc4 = loc4 + (JSON.serialize(loc6) + ":" + loc5);
                            }
                            return "{" + loc4 + "}";
                        }
                    }
                    return "null";
                }
                case "number":
                {
                    return isFinite(arg1) ? String(arg1) : "null";
                }
                case "string":
                {
                    loc3 = arg1.length;
                    loc4 = "\"";
                    loc2 = 0;
                    while (loc2 < loc3) 
                    {
                        loc1 = arg1.charAt(loc2);
                        if (loc1 >= " ") 
                        {
                            if (loc1 == "\\" || loc1 == "\"") 
                            {
                                loc4 = loc4 + "\\";
                            }
                            loc4 = loc4 + loc1;
                        }
                        else 
                        {
                            loc8 = loc1;
                            switch (loc8) 
                            {
                                case "":
                                {
                                    loc4 = loc4 + "\\b";
                                    break;
                                }
                                case "":
                                {
                                    loc4 = loc4 + "\\f";
                                    break;
                                }
                                case "\n":
                                {
                                    loc4 = loc4 + "\\n";
                                    break;
                                }
                                case "\r":
                                {
                                    loc4 = loc4 + "\\r";
                                    break;
                                }
                                case "\t":
                                {
                                    loc4 = loc4 + "\\t";
                                    break;
                                }
                                default:
                                {
                                    loc7 = loc1.charCodeAt();
                                    loc4 = loc4 + ("\\u00" + Math.floor(loc7 / 16).toString(16) + (loc7 % 16).toString(16));
                                }
                            }
                        }
                        loc2 = loc2 + 1;
                    }
                    return loc4 + "\"";
                }
                case "boolean":
                {
                    return String(arg1);
                }
                default:
                {
                    return "null";
                }
            }
        }
    }
